<?php
defined('BASEPATH') or exit('No direct script access allowed');
?>
<style>
    .custom-switch {
        padding-left: 3.25rem;
    }

    .custom-control {
        position: relative;
        display: block;
        min-height: 1.5rem;
        padding-left: 2.5rem;
    }

    .custom-control-input {
        position: absolute;
        z-index: -1;
        opacity: 0;
    }

    .custom-control-label {
        position: relative;
        margin-bottom: 0;
        vertical-align: top;
    }

    .custom-switch .custom-control-label::before {
        left: -3.25rem;
        width: 1.75rem;
        pointer-events: all;
        border-radius: .5rem;
    }

    .custom-control-label::before {
        position: absolute;
        top: .25rem;
        left: -2.5rem;
        display: block;
        width: 1rem;
        height: 1rem;
        pointer-events: none;
        content: "";
        background-color: #dee2e6;
        border: #adb5bd solid 1px;
        box-shadow: inset 0 0.25rem 0.25rem rgba(0, 0, 0, .1);
    }

    .custom-control-label::before, .custom-file-label, .custom-select {
        transition: background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
    }

    .custom-switch .custom-control-label::after {
        top: calc(.25rem + 2px);
        left: calc(-3.25rem + 2px);
        width: calc(1rem - 4px);
        height: calc(1rem - 4px);
        background-color: #adb5bd;
        border-radius: .5rem;
        transition: background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out, -webkit-transform .15s ease-in-out;
        transition: transform .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
        transition: transform .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out, -webkit-transform .15s ease-in-out;
    }

    .custom-control-input:checked ~ .custom-control-label::before {
        color: #fff;
        border-color: #007bff;
        background: #007bff linear-gradient(180deg, #268fff, #007bff) repeat-x;
        box-shadow: none;
    }

    .custom-switch .custom-control-input:checked ~ .custom-control-label::after {
        background-color: #dee2e6;
        -webkit-transform: translateX(.75rem);
        transform: translateX(.75rem);
    }

    .custom-control-label::after {
        position: absolute;
        top: .25rem;
        left: -2.5rem;
        display: block;
        width: 1rem;
        height: 1rem;
        content: "";
        background: no-repeat 50%/50% 50%;
    }
</style>

<div class="row">
    <div class="col-sm-12">
        <div class="box">
            <div class="box-header with-border">
                <div class=" col-sm-6">
                    <?php echo form_open(current_url(), array('class' => 'form-inline', 'method' => 'get', 'id' => 'search_goods')); ?>

                    <div class="form-group" style="margin-right:15px;">
                        <select class="form-control" name="sitetype">
                            <option value="0">所有类型</option>
                            <?php foreach ($sitetype as $k => $s): ?>
                                <option value="<?php echo $k; ?>"><?php echo $s; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="form-group" style="margin-right:15px;">

                        <div class="input-group input-group-sm">
                            <?php echo form_input($keyword); ?>

                        </div>
                    </div>
                    <div class="form-group">
                        <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => "搜索")); ?>
                    </div>


                    <?php echo form_close(); ?>
                </div>
                <h3 class="box-title pull-right"><?php //echo anchor('admin/tbshop/create', '<i class="fa fa-plus"></i> 添加目录', array('class' => 'btn btn-block btn-sm btn-primary btn-flat')); ?>
                    <?php
                    if ($trash == 1):
                        ?>
                        <a href="/backend/sites/site" class="btn btn-block btn-sm btn-primary btn-flat">网站列表</a>
                    <?php else: ?>
                        <a href="/backend/sites/site?trash=1" class="btn btn-block btn-sm btn-primary btn-flat">回收站</a>
                    <?php endif; ?>
                </h3>
            </div>
            <div class="box-body">
                <?php if ($data): ?>
                    <table class="table table-striped table-hover" style="margin-top:20px;">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>名称</th>
                            <th>域名</th>
                            <!--<th>备注</th>-->
                            <th>最新询盘</th>
                            <th>同步数据</th>
                            <th>类型</th>
                            <th>商品数</th>
                            <th>询盘数</th>
                            <th>备注</th>
                            <th>管理</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($data as $item): ?>
                            <tr shopid="<?php echo $item["id"]; ?>">
                                <td><?php echo $item["id"]; ?></td>
                                <td ondblclick="edit(this,'name')"><?php echo $item["name"]; ?></td>
                                <td><a href="<?php echo $item["domian"]; ?>"
                                       target="_blank"><?php echo $item["domian"]; ?></a></td>
                                <!--<td ondblclick="edit(this,'memo')" ><?php echo $item["memo"]; ?></td>-->

                                <td><?php echo $item["lastorder"] ? $item["lastorder"] : "暂无记录"; ?></td>
                                <td>

                                    <div class="custom-control custom-switch" style="margin-left:10px;"
                                         cid="<?php echo $item["id"]; ?>">
                                        <input type="checkbox" class="custom-control-input syncbox"
                                               id="toogle<?php echo $item["id"]; ?>" <?php if ($item["issync"] == 1) echo " checked"; ?>>
                                        <label class="custom-control-label"
                                               for="toogle<?php echo $item["id"]; ?>"></label>
                                    </div>

                                </td>
                                <td><?php echo $sitetype[$item["sitetype"]]; ?></td>
                                <td><?php echo $item["gcount"]; ?></td>
                                <td><?php echo $item["ocount"]; ?></td>
                                <td shopid="<?php echo $item["id"]; ?>"><span
                                            ondblclick="edit(this,'memo');" <?php if ($item["memo"]) echo 'style="color:red;"'; ?>><?php echo $item["memo"] ? htmlentities($item["memo"]) : "双击添加备注"; ?></span>
                                </td>
                                <td>
                                    <?php echo anchor('backend/sites/category/index/' . $item["id"], '<i class="fa fa-bars fa-lg"></i> 分类', 'class="btn btn-xs btn-primary"'); ?>
                                    &nbsp&nbsp
                                    <?php echo anchor('backend/sites/product/index/' . $item["id"], '<i class="fa fa-cloud fa-lg"></i> 产品', 'class="btn btn-xs btn-info"'); ?>
                                    &nbsp&nbsp

                                    <?php echo anchor('backend/sites/order/index/' . $item["id"], '<i class="fa fa-shopping-cart fa-lg"></i> 询盘', 'class="btn btn-xs btn-danger"'); ?>
                                    &nbsp&nbsp


                                    <?php
                                    switch ($item["sitetype"]) {
                                        case 1:
                                            $adminlink = $item["domian"] . '?_dreamtoken=' . $item["apipass"];
                                            break;

                                    }
                                    echo anchor($adminlink, '<i class="fa fa-send fa-lg"></i> 后台', 'target="_blank" class="btn btn-xs bg-purple"'); ?>
                                    &nbsp&nbsp

                                    <button class="btn btn-xs bg-maroon"
                                            onclick="modify('<?php echo $item["name"]; ?>',<?php echo $item["id"]; ?>);">
                                        <i class="fa fa-edit fa-lg"></i> 配置
                                    </button>


                                </td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>

                <?php
                if (empty($data)) {
                    echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                }
                ?>
                <?php echo $pager; ?>

                <div class=" pull-right form-inline" style="line-height:30px;margin-top:25px;">
                    <?php echo '共' . $total . '条产品，' . $page . '/' . $pages . '页'; ?>
                </div>

            </div>
        </div>
    </div>
</div>


<?php echo form_open(current_url(), array('class' => 'form-horizontal', 'onsubmit' => 'return checkform();', 'id' => 'form-create_notice')); ?>
<div class="row">
    <div class="col-sm-12">
        <div class="box">

            <div class="box-header with-border">
                <h3 class="box-title">添加网站</h3>
            </div>

            <div class="box-body">

                <div class="row">
                    <div class="col-xs-3">
                        <input type="text" class="form-control" name="name" id="name" placeholder="网站名称">
                    </div>

                    <div class="col-xs-3">
                        <input type="text" class="form-control" name="domian" id="domian" placeholder="域名，包含http">
                    </div>

                    <div class="col-xs-3">
                        <select name="sitetype" class="form-control" id="sitetype">
                            <?php
                            foreach ($sitetype as $k => $v) {
                                echo '<option value="' . $k . '">' . $v . '</option>';
                            }
                            ?>
                        </select>
                    </div>
                    <div class="col-xs-3">
                        <div class="input-group">
                            <input type="text" class="form-control" name="apipass" placeholder="API接口密钥">
                            <span class="input-group-btn">
                                <button class="btn btn-success btn-flat genbtn">生成</button>
                            </span>
                        </div>
                    </div>
                </div>
                <div class="row" style="margin-top:10px;">
                    <div class="col-xs-12">
                        <input type="text" class="form-control" name="memo" id="memo" placeholder="备注">
                    </div>
                </div>

                <div class="row" style="margin-top:10px;">
                    <div class="col-xs-12">
                        <i class="fa fa-book margin-r-5"></i>
                    </div>
                </div>
                <div class="row" style="margin-top:10px;">
                    <div class="col-xs-3">
                        <input type="hidden" name="action" value="add">
                        <input type="submit" class="btn btn-flat btn-primary btn-sm" value="添加网站">
                    </div>
                </div>

            </div>


        </div>
    </div>
    <!-- /.box-body -->
</div>
<?php echo form_close(); ?>
<script>


    $("input.syncbox").on("click", function () {
        var cid = $(this).parent().attr("cid");
        if ($(this).is(":checked")) {
            newval = 1;
        } else {
            newval = 0;
        }
        $.ajax({
            type: "post",
            url: "/backend/sites/site/ajaxedit",
            dataType: "json",
            data: {"attrName": "issync", "newHtml": newval, "cid": cid},
            error: function () {
            },
            success: function () {
            },
        });
    });


    var pasArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-', '$', '%', '@', '+', '!'];

    function passwords(pasLen) {
        var password = '';
        var pasArrLen = pasArr.length;
        for (var i = 0; i < pasLen; i++) {
            var x = Math.floor(Math.random() * pasArrLen);
            password += pasArr[x];
        }
        console.log(password);
        return password;
    }

    $("button.genbtn").on("click", function () {
        let pass = passwords(12);
        $(this).parent().parent().find("input[name=apipass]").val(pass);
        return false;
    });


    function edit(obj, attrName) {

        var oldHtml = obj.innerHTML;
        var hint = '';
        if (oldHtml == "双击添加备注") {
            hint = oldHtml;
            oldHtml = '';
        }
        var newobj = document.createElement('input');
        newobj.name = attrName;
        newobj.type = "text";
        newobj.setAttribute("class", "form-control input-sm");
        newobj.value = oldHtml;
        var cid = $(obj).parent().attr("shopid");
        obj.innerHTML = '';
        obj.appendChild(newobj);
        newobj.focus();

        $(newobj).keydown(function (event) {
            if (event.keyCode == 13) {
                $(this).trigger("blur");
            }
        });

        $(newobj).blur(function () {
            if (this.value == null || this.value == "") {
                if (hint) {
                    obj.innerHTML = hint;
                } else {
                    obj.innerHTML = oldHtml;
                }

                return false;
            }
            obj.innerHTML = this.value ? this.value : oldHtml;
            if (oldHtml != this.value) {
                $.ajax({
                    type: "post",
                    url: "/backend/sites/site/ajaxedit",
                    dataType: "json",
                    data: {"attrName": attrName, "newHtml": this.value, "cid": cid},
                    error: function () {
                    },
                    success: function () {
                    },
                });
            }

        })
    }

    var modify = function (title, oid) {
        layer.open({
            type: 2,
            title: title + ' 修改配置',
            shadeClose: true,
            shade: 0.8,
            area: ['74%', '70%'],
            content: '/backend/sites/site/modify/' + oid
        });
    }


    var checkform = function () {
        checkfild = {
            "name": "网站名称",
            "domain": "域名",
            "note": "备注",
            "apipass": "API密钥",
        };
        for (var index in checkfild) {
            if ($("input[name='" + index + "']").val().trim().length == 0) {
                layer.msg(checkfild[index] + "不能为空");
                return false;
            }
        }
    };

    $("#sitetype").on("change", function () {
        $(".site_extra").hide();
        if ($("#type" + $("#sitetype").val()).length > 0) {
            $("#type" + $("#sitetype").val()).show();
        }
    });


    $("button.btn-delete").on("click", function () {
        var cid = $(this).attr("itemid");
        var row = $(this).parent().parent();
        layer.confirm("确认要删除吗，删除将无法回复？", {title: "确认"}, function (index) {
            $.ajax({
                type: "post",
                url: "/backend/products/categoryajaxdel",
                dataType: "json",
                data: {"cid": cid},
                error: function () {
                },
                success: function (result) {
                    if (result["status"] == 1) {
                        row.remove();
                    } else {
                        layer.msg(result["msg"]);
                    }
                },
            });
            layer.close(index);
        });
    });


    $(function () {
        require(['jquery', 'clipboard.min'], function ($, clipboardlib) {
            var clipboard = new clipboardlib('.copybtn');
            clipboard.on('success', function (e) {
                layer.msg("复制成功");
            });
            clipboard.on('error', function (e) {
                layer.msg("复制失败");
            });
        });
    });
</script>

<?php if ($isadmin == 1): ?>
    <script>
        $("body").on("click", "td.lockcol", function () {
            var shopid = $(this).parent().attr("shopid");
            var shopname = $(this).parent().find("td.shopname").text();
            layer.confirm("确认要锁定分类 " + shopname + " 吗？", {title: "确认"}, function (index) {
                url = "/backend/products/togglelock/" + shopid;
                $.get(url, {},
                    function (data) {
                        layer.close(index);
                        window.location.href = window.location.href;
                    });
            });
            console.log(shopid);
        });
    </script>
<?php endif; ?>
